package com.xxx.car.web.api;

import com.xxx.car.cache.CarRedisTemplate;
import com.xxx.car.constants.RedisConstants;
import com.xxx.car.entity.vo.request.ReqPushRegister;
import com.xxx.car.redisCluster.utils.RedisKey;
import com.xxx.common.base.enums.ApiResponseEnum;
import com.xxx.common.base.vo.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by wenjiexu on 2017/7/5.
 */
@Controller
@RequestMapping("/api/push")
public class ApiPushController {

    @Autowired
    private CarRedisTemplate carRedisTemplate;

    @RequestMapping("/regist")
    @ResponseBody
    public APIResponse regist(@Valid @ModelAttribute ReqPushRegister reqPushRegister, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        carRedisTemplate.set(new RedisKey(RedisConstants.USER_DEVICE_TOKEN, String.valueOf(reqPushRegister.getUserId())), reqPushRegister.getDeviceToken());
        return APIResponse.returnSuccess();
    }

}
